Imports System.ComponentModel

Public Class BLDisposer
    Implements IDisposable

    'ref: http://msdn.microsoft.com/en-us/library/system.idisposable.dispose(v=vs.71).aspx
    'ref: http://msdn.microsoft.com/en-us/library/b1yfkh5e(v=vs.71).aspx

    ' Pointer to an external unmanaged resource.
    Private handle As IntPtr
    ' Other managed resource this class uses.
    Public component As New Component
    ' Track whether Dispose has been called.
    Private disposed As Boolean = False

    ' The class constructor.
    Public Sub New(ByVal handle As IntPtr)
        Me.handle = handle
    End Sub

    ''' <summary>
    ''' Implement IDisposable.
    ''' Do not make this method virtual.
    ''' A derived class should not be able to override this method.
    ''' </summary>
    ''' <remarks></remarks>
    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        ' This object will be cleaned up by the Dispose method.
        ' Therefore, you should call GC.SupressFinalize to
        ' take this object off the finalization queue 
        ' and prevent finalization code for this object
        ' from executing a second time.
        GC.SuppressFinalize(Me)
    End Sub

    ''' <summary>
    ''' Dispose(bool disposing) executes in two distinct scenarios.
    ''' If disposing equals true, the method has been called directly
    ''' or indirectly by a user's code. Managed and unmanaged resources can be disposed.
    ''' If disposing equals false, the method has been called by the
    ''' runtime from inside the finalizer and you should not reference
    ''' other objects. Only unmanaged resources can be disposed.
    ''' </summary>
    ''' <param name="disposing">
    '''       TRUE: dispose all managed and unmanaged resources
    '''       FALSE: dispose only unmanaged resources
    ''' </param>
    ''' <remarks></remarks>
    Private Overloads Sub Dispose(ByVal disposing As Boolean)
        ' Check to see if Dispose has already been called.
        If Not Me.disposed Then
            ' If disposing equals true, dispose all managed 
            ' and unmanaged resources.
            If disposing Then
                ' Dispose managed resources.
                component.Dispose()
            End If

            ' Call the appropriate methods to clean up 
            ' unmanaged resources here.
            ' If disposing is false, 
            ' only the following code is executed.
            CloseHandle(handle)
            handle = IntPtr.Zero
        End If
        disposed = True
    End Sub

    ''' <summary>
    ''' Use interop to call the method necessary to clean up the unmanaged resource.
    ''' </summary>
    ''' <param name="handle"></param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    <System.Runtime.InteropServices.DllImport("Kernel32")> _
    Private Shared Function CloseHandle(ByVal handle As IntPtr) As [Boolean]
    End Function

    ''' <summary>
    ''' This finalizer will run only if the Dispose method does not get called.
    ''' It gives your base class the opportunity to finalize.
    ''' Do not provide finalize methods in types derived from this class.
    ''' </summary>
    ''' <remarks></remarks>
    Protected Overrides Sub Finalize()
        ' Do not re-create Dispose clean-up code here.
        ' Calling Dispose(false) is optimal in terms of
        ' readability and maintainability.
        Dispose(False)
        MyBase.Finalize()
    End Sub

    ''' <summary>
    ''' Release COM objects
    ''' </summary>
    ''' <param name="COM_Object">COM object to be released</param>
    ''' <remarks></remarks>
    Public Shared Sub ReleaseObject(ByVal COM_Object As Object)
        Try
            System.Runtime.InteropServices.Marshal.ReleaseComObject(COM_Object)
        Finally
            COM_Object = Nothing
            GC.Collect()
        End Try
    End Sub
End Class
